package com.sylar.happyreader;

import java.io.Serializable;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.sylar.happyreader.bean.BookItem;
import com.sylar.happyreader.view.SlidingMenuView;
import com.sylar.happyreader.view.VerticalTextView;

public class MainTabActivity extends TabActivity implements OnClickListener,OnTabChangeListener {
	private SlidingMenuView slidingMenuView;
	protected Resources mRes;
	private TabHost mTabHost;
//	private ViewGroup tabcontent;

	private VerticalTextView vtv_man;
	private VerticalTextView vtv_woman;
	private VerticalTextView vtv_local;
	private VerticalTextView vtv_about;
	private VerticalTextView vtv_marklets;

	private List<BookItem> books;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.activity_main);
		
		books = (List<BookItem>) this.getIntent().getSerializableExtra("books");
		
		mRes = getResources();

		mTabHost = (TabHost) findViewById(android.R.id.tabhost);

		mTabHost.setOnTabChangedListener(this);
		
		Intent man = new Intent(this, ManBookListActivity.class);
		man.putExtra("sex", "man");
		
		Intent woman = new Intent(this, ManBookListActivity.class);
		woman.putExtra("sex", "woman");
		
		mTabHost.addTab(mTabHost.newTabSpec("RADIO_MAN").setIndicator("radio_man").setContent(man));
		mTabHost.addTab(mTabHost.newTabSpec("RADIO_WOMAN").setIndicator("radio_woman").setContent(woman));
		mTabHost.addTab(mTabHost.newTabSpec("RADIO_MARKLETS").setIndicator("radio_marklets").setContent(new Intent(this, ManBookListActivity.class)));
		mTabHost.addTab(mTabHost.newTabSpec("RADIO_LOCAL").setIndicator("radio_local").setContent(new Intent(this, LocalBookActivity.class)));
		mTabHost.addTab(mTabHost.newTabSpec("RADIO_ABOUT").setIndicator("radio_about").setContent(new Intent(this, LocalBookActivity.class)));
		
		Intent bookgrid = new Intent(this, BookGridActivity.class);
		bookgrid.putExtra("books", (Serializable)books);
		mTabHost.addTab(mTabHost.newTabSpec("RADIO_BOOKGRID").setIndicator("radio_bookgrid").setContent(bookgrid));
		
		slidingMenuView = (SlidingMenuView) findViewById(R.id.sliding_menu_view);

		ImageView btn_menu = (ImageView) findViewById(R.id.btn_t_menu);
		btn_menu.setOnClickListener(this);

		vtv_man = (VerticalTextView) findViewById(R.id.vtv_man);
		vtv_woman = (VerticalTextView) findViewById(R.id.vtv_woman);
		vtv_local = (VerticalTextView) findViewById(R.id.vtv_local);
		vtv_about = (VerticalTextView) findViewById(R.id.vtv_about);
		vtv_marklets = (VerticalTextView) findViewById(R.id.vtv_marklets);

		vtv_marklets.setOnClickListener(this);
		vtv_man.setOnClickListener(this);
		vtv_woman.setOnClickListener(this);
		vtv_local.setOnClickListener(this);
		vtv_about.setOnClickListener(this);

		mTabHost.setCurrentTabByTag("RADIO_BOOKGRID");
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.vtv_man:
			do_getManList();
			break;
		case R.id.vtv_woman:
			do_getWoManList();
			break;
		case R.id.vtv_local:
			do_getLocalBook();
			break;
		case R.id.vtv_about:
			do_aboutUs();
			break;

		case R.id.vtv_marklets:
			to_myMarklets();
			break;

		case R.id.btn_t_menu:
			changeView();
			break;
		}
	}

	void to_myMarklets() {
		vtv_marklets.setBackgroundResource(R.drawable.menu_bg);
		vtv_man.setBackgroundDrawable(null);
		vtv_about.setBackgroundDrawable(null);
		vtv_local.setBackgroundDrawable(null);
		vtv_woman.setBackgroundDrawable(null);
	}

	void do_getManList() {
		mTabHost.setCurrentTabByTag("RADIO_MAN");
		
		vtv_man.setBackgroundResource(R.drawable.menu_bg);
		vtv_about.setBackgroundDrawable(null);
		vtv_local.setBackgroundDrawable(null);
		vtv_woman.setBackgroundDrawable(null);
		vtv_marklets.setBackgroundDrawable(null);
		
//		Intent i = new Intent(this, ManBookListActivity.class);
//		i.putExtra("sex", "man");
//		View v = getLocalActivityManager().startActivity("man", i).getDecorView();
//		tabcontent.removeAllViews();
//		tabcontent.addView(v);
		
	}

	void do_getWoManList() {
		mTabHost.setCurrentTabByTag("RADIO_WOMAN");
		vtv_man.setBackgroundDrawable(null);
		vtv_marklets.setBackgroundDrawable(null);
		vtv_woman.setBackgroundResource(R.drawable.menu_bg);
		vtv_about.setBackgroundDrawable(null);
		vtv_local.setBackgroundDrawable(null);
//		Intent i = new Intent(this, ManBookListActivity.class);
//		i.putExtra("sex", "woman");
//		View v = getLocalActivityManager().startActivity("woman", i).getDecorView();
//		tabcontent.removeAllViews();
//		tabcontent.addView(v);
	}

	void do_getLocalBook() {
//		Intent i = new Intent(this, LocalBookActivity.class);
//		View v = getLocalActivityManager().startActivity(LocalBookActivity.class.getName(), i).getDecorView();
//		tabcontent.removeAllViews();
//		tabcontent.addView(v);
		mTabHost.setCurrentTabByTag("RADIO_LOCAL");
		vtv_man.setBackgroundDrawable(null);
		vtv_marklets.setBackgroundDrawable(null);
		vtv_woman.setBackgroundDrawable(null);
		vtv_about.setBackgroundDrawable(null);
		vtv_local.setBackgroundResource(R.drawable.menu_bg);
	}

	void do_aboutUs() {
		mTabHost.setCurrentTabByTag("RADIO_ABOUT");
		vtv_man.setBackgroundDrawable(null);
		vtv_marklets.setBackgroundDrawable(null);
		vtv_woman.setBackgroundDrawable(null);
		vtv_about.setBackgroundResource(R.drawable.menu_bg);
		vtv_local.setBackgroundDrawable(null);
	}
	
	void toBookGridView(){
//		Intent i = new Intent(this, BookGridActivity.class);
//		i.putExtra("books", (Serializable)books);
//		View v = getLocalActivityManager().startActivity(BookGridActivity.class.getName(), i).getDecorView();
//		tabcontent.removeAllViews();
//		tabcontent.addView(v);
	}

	void changeView() {
		if (slidingMenuView != null && slidingMenuView.getCurrentScreen() == 0)
			slidingMenuView.snapToScreen(1);
		else
			slidingMenuView.snapToScreen(0);
	}

	// 拦截MENU按钮
	public boolean onMenuOpened(int featureId, Menu menu) {
		changeView();
		return false;
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("menu");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (slidingMenuView != null && slidingMenuView.getCurrentScreen() == 0) {
			slidingMenuView.snapToScreen(1);
		} else {
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				new AlertDialog.Builder(MainTabActivity.this).setTitle("小说书城提示").setMessage("确定退出使用吗?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						android.os.Process.killProcess(android.os.Process.myPid());
						System.exit(1);
					}
				}).setNegativeButton("再看一会儿", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				}).show();
			}
			return false;
		}
		return true;
	}

	public void onTabChanged(String tabId) {
		Activity activity = getLocalActivityManager().getActivity(tabId);
		if (activity != null) {
			activity.onWindowFocusChanged(true);
		}
		mTabHost.setCurrentTabByTag(tabId);
	}

}
